package chapter20;

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.ListIterator;

/**
 * @program: exercise
 * @description: 数组线性表类Arraylist和链表类LinkedList
 * @author: 陈庆彪
 * @create: 2020-02-21 10:41
 **/
public class TestArrayAndLinkedList {
    public static void main(String[] args) {
        List<Integer> arrayList=new ArrayList<>();
        arrayList.add(1);
        arrayList.add(2);
        arrayList.add(3);
        arrayList.add(1);
        arrayList.add(4);
        arrayList.add(0);
        arrayList.add(0,10);
        arrayList.add(3,30);
        System.out.println("\nA list of integers in the array list:");
        System.out.println(arrayList);

        LinkedList<Object> linkedList= new LinkedList<>(arrayList);
        linkedList.add(1,"red");
        linkedList.removeLast();
        linkedList.addFirst("green");

        System.out.println("\nDisplay the linked list forward: ");
        ListIterator<Object> listIterator=linkedList.listIterator();
        while (listIterator.hasNext()){
            System.out.print(listIterator.next()+" ");
        }
        System.out.println();

        System.out.println("\nDisplay the linked list backward: ");
        listIterator=linkedList.listIterator(linkedList.size());
        while (listIterator.hasPrevious()){
            System.out.print(listIterator.previous()+" ");
        }


    }
}
